package com.twq.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket docket(Environment environment) {
        // 设置要显示的Swagger环境
        Profiles profiles = Profiles.of("dev", "test");
        // 通过 environment.acceptsProfiles(profiles) 判断是否处在自己设定的环境当中
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(setMyApiInfo())
                //此处判断是生产环境 flag 就是为true 就可以访问Swagger
                .enable(flag)
                .select()
                //这里只扫描controller包下面的文件，默认的error接口就不会被扫描
                .apis(RequestHandlerSelectors.basePackage("com.twq.controller"))
                //.paths(PathSelectors.ant("/kuang/**"))
                .build()//build
                .groupName("谭文强");
    }

    @Bean
    public Docket docket1() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("A");
    }

    @Bean
    public Docket docket2() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("B");
    }

    @Bean
    public Docket docket3() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("C");
    }


    /**
     * 配置swagger2 apiInfo信息
     *
     * @return
     */
    private ApiInfo setMyApiInfo() {
        return new ApiInfo("谭文强的Swagger文档",
                "Api Documentation",
                "1.0",
                "urn:tos",
                new Contact("rsy", "123", "123@qq.com"),
                "Apache 2.0",
                "http://www.baidu.com",
                new ArrayList());
    }


}
